<?php

class Order_Model_Create{
	
	const EMPTY_CART = "There are no items in the cart";
	
	protected $userId;
	
	protected $_cartModel;
	
	protected $_orderModel;
	
	public function init(){
		$this->_cartModel = new Buyback_Model_Cart();
	}
	
	public function __construct(){
		$this->init();
		
		if(Zend_Auth::getInstance()->hasIdentity()){
			$this->userId = Zend_Auth::getInstance()->getIdentity()->id;
		}
		$this->_orderModel = new Doctrine_Model_Orders();
	}
	
	public function createOrder(){
		if(!$this->_cartModel->hasItem())
			throw new Exception(self::EMPTY_CART);
		$this->_orderModel->userId = $this->userId;
		$this->_orderModel->payId = Doctrine_Model_UserPayments::getPayId($this->userId);
		$this->_orderModel->total = $this->_cartModel->getOfferPrice();
		$this->_orderModel->adjustedTotal = $this->_cartModel->getOfferPrice();
		$this->_orderModel->save();
		
	}
	
	public function createOrderItems(){
		$order = new Order_Resource_Item($this->_orderModel->id);
		$order->saveOrderItems();
	}
}